package com.ruoyi.repairUnit.service.impl;

import com.ruoyi.project.domain.HouseProjectT;
import com.ruoyi.project.domain.HouseRepairCheckT;
import com.ruoyi.repairArea.domain.HouseRepairAreaT;
import com.ruoyi.repairArea.mapper.HouseRepairAreaTMapper;
import com.ruoyi.repairUnit.domain.HouseRepairUnitT;
import com.ruoyi.repairUnit.mapper.HouseRepairUnitTMapper;
import com.ruoyi.repairUnit.service.IHouseRepairUnitTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 户资金修改Service业务层处理
 *
 * @author tyq
 * @date 2023-05-24
 */
@Service
public class HouseRepairUnitTServiceImpl implements IHouseRepairUnitTService
{
    @Autowired
    private HouseRepairUnitTMapper houseRepairUnitTMapper;
    @Autowired
    private HouseRepairAreaTMapper houseRepairAreaTMapper;


    /**
     * 查询户资金修改
     *
     * @param doorId 户资金修改主键
     * @return 户资金修改
     */
    @Override
    public HouseRepairUnitT selectHouseRepairUnitTByDoorId(Long doorId)
    {
        return houseRepairUnitTMapper.selectHouseRepairUnitTByDoorId(doorId);
    }

    /**
     * 查询户资金修改列表
     *
     * @param houseRepairUnitT 户资金修改
     * @return 户资金修改
     */
    @Override
    public List<HouseRepairUnitT> selectHouseRepairUnitTList(HouseRepairUnitT houseRepairUnitT)
    {
        return houseRepairUnitTMapper.selectHouseRepairUnitTList(houseRepairUnitT);
    }

    @Override
    public HouseRepairUnitT selectHouseRepairUnitSum(HouseRepairUnitT houseRepairUnitT)
    {
        return houseRepairUnitTMapper.selectHouseRepairUnitSum(houseRepairUnitT);
    }

    /**
     * 新增户资金修改
     *
     * @param houseRepairUnitT 户资金修改
     * @return 结果
     */
    @Override
    public int insertHouseRepairUnitT(HouseRepairUnitT houseRepairUnitT)
    {
        return houseRepairUnitTMapper.insertHouseRepairUnitT(houseRepairUnitT);
    }

    /**
     * 修改户资金修改
     *
     * @param  houseRepairUnitVO,  houseRepairCheckO, houseProjectT 户资金修改
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHouseRepairUnitT(HouseRepairUnitT houseRepairUnitVO, HouseRepairCheckT houseRepairCheckO,HouseProjectT houseProjectT)
    {
        houseRepairUnitTMapper.insertRepairCheck(houseRepairCheckO);
        houseRepairUnitTMapper.updateRepairMoneyForCheck(houseRepairUnitVO);
        HouseRepairAreaT repairVO=houseRepairAreaTMapper.sumNoMoney(houseProjectT);
        houseProjectT.setPayMoney(repairVO.getPayMoney());
        return houseRepairUnitTMapper.updateProjectMoneyForCheck(houseProjectT);
    }

    /**
     * 核算维修项目
     *
     * @param projectVO,repairListTemp 核算维修项目主键
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProjectAndRepair(HouseProjectT projectVO, List<HouseRepairUnitT> repairListTemp)
    {
        for(HouseRepairUnitT vo:repairListTemp){
            houseRepairUnitTMapper.updateRepair(vo);
        }
        HouseRepairAreaT projectTemp = new HouseRepairAreaT();
        projectTemp=houseRepairAreaTMapper.sumNoMoney(projectVO);
        projectVO.setNoMoney(projectTemp.getNoMoney());
        return houseRepairUnitTMapper.updateProjectForTrueMoney(projectVO);
    }

    public HouseRepairUnitT selectHouseProjectTByProjectId(HouseRepairUnitT houseRepairUnitT)
    {
        return houseRepairUnitTMapper.selectHouseProjectTByProjectId(houseRepairUnitT);
    }

    public HouseRepairUnitT getRepairDetail(HouseRepairUnitT houseRepairUnitT)
    {
        return houseRepairUnitTMapper.getRepairDetail(houseRepairUnitT);
    }

}
